package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Resource
    private ClazzService clazzService;

    @Override
    public PageResult<Student> pageStudent(StudentQueryParam student) {
        Page<Student> page = Page.of(student.getPage(), student.getPageSize());
        Page<Student> studentPage = lambdaQuery().like(student.getName() != null && !student.getName().isEmpty(), Student::getName, student.getName()).
                eq(student.getDegree() != null, Student::getDegree, student.getDegree()).
                eq(student.getClazzId() != null, Student::getClazzId, student.getClazzId()).page(page);
        List<Student> students = studentPage.getRecords();
        List<Integer> clazzIds = students.stream().map(Student::getClazzId).toList();

        if(!clazzIds.isEmpty()){
            List<Clazz> clazz = clazzService.listByIds(clazzIds);
            Map<Integer, String> clazzMap = clazz.stream().collect(Collectors.toMap(Clazz::getId, Clazz::getName));
            students.forEach(stu->stu.setClazzName(clazzMap.get(stu.getClazzId())));
        }
        return new PageResult<>(studentPage.getPages(),students);
    }

    @Override
    public void saveStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(student.getCreateTime());
        save(student);
    }

    @Override
    public Student getStudent(Integer id) {
      return getById(id);
    }

    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        updateById(student);
    }

    @Override
    public void deleteS(List<Integer> ids) {
            removeBatchByIds(ids);
    }

    @Override
    public void addScore(Integer id, Integer score) {
        Student student = getById(id);
        if(score>0) {
            student.setViolationScore((short) (student.getViolationScore() + score));
            student.setViolationCount((short) (student.getViolationCount()+1));
        }
        updateById(student);
    }

    @Override
    public StudentCount countStu() {
        List<Clazz> clazzList = clazzService.list();
       // Map<Integer, String> clazzMap = clazzList.stream().collect(Collectors.toMap(Clazz::getId, Clazz::getName));
        Map<Integer, String> clazzMap = clazzList.stream().collect(Collectors.toMap(clazz -> clazz.getId(), clazz -> clazz.getName()));
        ArrayList<String> name = new ArrayList<>();
        ArrayList<Long> data = new ArrayList<>();
        for (Integer id : clazzMap.keySet()) {
            //获取每个班级的人数
            Long count = lambdaQuery().eq(Student::getClazzId, id).count();
            name.add(clazzMap.get(id));
            data.add(count);
        }
            return new StudentCount(name,data);
    }
    @Override
    public List<Map<String,Object>> countDegree() {
        ArrayList<Map<String, Object>> mapArrayList = new ArrayList<>();
        Map<Integer, String> dreegeMap = Map.of
                (1, "初中", 2, "高中", 3, "大专", 4, "本科", 5, "硕士",6,"博士");
        for (Integer id : dreegeMap.keySet()) {
            //
            Long count = lambdaQuery().eq(Student::getDegree, id).count();

            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("name",dreegeMap.get(id));
            hashMap.put("value",count);
            mapArrayList.add(hashMap);
        }
        return mapArrayList;
    }
}
